﻿using System;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.NLogSdk;
using ThresholdAssistant.WebAPI.Models.Enums;
using ThresholdAssistant.WebAPI.Repository;

namespace ThresholdAssistant.WebAPI.Service
{
    /// <summary>
    /// 数据清理服务
    /// </summary>
    public class DataClearService : BaseService
    {
        /// <summary>
        /// 配置仓储
        /// </summary>
        public ConfigRepository ConfigRepository { get; set; }

        /// <summary>
        /// 数据仓储
        /// </summary>
        public RecordRepository RecordRepository { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task ExecuteAsync()
        {
            var datetime = DateTime.Now;
            var list = await ConfigRepository.GetListByConfigTypeAsync(ConfigTypeEnum.DataClearType);
            if (list != null && list?.Any() == true)
            {
                foreach (var item in list)
                {
                    var blClear = false;
                    //不清理
                    if (item.ConfigValue == "0")
                    {
                        return;
                    }
                    //每日
                    if (item.ConfigValue == "1")
                    {
                        blClear = true;
                    }
                    //每周
                    if (item.ConfigValue == "2")
                    {
                        //判断是否是周日
                        if (datetime.DayOfWeek == DayOfWeek.Sunday)
                        {
                            blClear = true;
                        }
                        else
                        {
                            NLogUtil.Info($"DataClearService:不是每周周日,不执行清理数据逻辑:{datetime.DayOfWeek};group_id:{item.GroupId}");
                            return;
                        }
                    }
                    //每月
                    if (item.ConfigValue == "3")
                    {
                        //判断是否是周日
                        if (datetime.Day == 1)
                        {
                            blClear = true;
                        }
                        else
                        {
                            NLogUtil.Info($"DataClearService:不是每月1号,不执行清理数据逻辑:{datetime.Day};group_id:{item.GroupId}");
                            return;
                        }
                    }
                    if (blClear)
                    {
                        await ClearDataAsync(item.GroupId);
                    }
                }
            }
            NLogUtil.Info($"DataClearService执行完毕");
        }

        /// <summary>
        /// 清理数据逻辑
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        private async Task ClearDataAsync(long groupId)
        {
            //删除数据逻辑
            NLogUtil.Info($"清理数据逻辑执行中:{groupId}");
            try
            {
                await RecordRepository.DeletesByGroupIdAsync(groupId);
            }
            catch (Exception ex)
            {
                NLogUtil.Info($"清理数据逻辑执行异常：{groupId},ex:{ex}");
            }
            NLogUtil.Info($"清理数据逻辑执行完毕:{groupId}");
        }
    }
}
